<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>6_getSnapshotBeforeUpdate_demo</title>
    <style>
        .list{
            width: 200px;
            height: 150px;
            background-color: skyblue;
            overflow: auto;
        }
        .news{
            height: 30px;
        }
    </style>
</head>
<body>
    <div id="test">

    </div>
    <!-- 引入react核心库 -->
    <script crossorigin src="../17.0.1/react.development.js"></script>
    <!-- 引入react-dom，用于支持react操作DOM -->
    <script crossorigin src="../17.0.1/react-dom.development.js"></script>
    <!-- 引入babel，用于将jsx转为js -->
    <script type="text/javascript" src="https://unpkg.com/babel-standalone@6.26.0/babel.js"></script>
    <script type="text/javascript" src="https://unpkg.com/prop-types@15.6.2/prop-types.js"></script> 
    <script type="text/babel">
        
        //创建组件
        class NewsList extends React.Component {
            /*
                1. 初始化阶段: 由ReactDOM.render()触发---初次渲染
                                1. constructor()
                                2. getDerivedStateFromProps
                                3. render()
                                4. componentDidMount() =====> 常用
                                            一般在这个钩子中做一些初始化的事，例如：开启定时器、发送网络请求、订阅消息
                2. 更新阶段: 由组件内部this.setSate()或父组件重新render触发
                                1. getDerivedStateFromProps
                                2. shouldComponentUpdate()
                                3. render()
                                4. getSnapshotBeforeUpdate
                                5. componentDidUpdate()
                3. 卸载组件: 由ReactDOM.unmountComponentAtNode()触发
                                1. componentWillUnmount()  =====> 常用
                                            一般在这个钩子中做一些收尾的事，例如：关闭定时器、取消订阅消息
            */

            state = {
                newsArr:[]
            }
            componentDidMount(){
                setInterval(()=>{
                    const {newsArr} = this.state
                    const news = '新闻'+(newsArr.length+1)
                    this.setState({
                        newsArr:[news,...newsArr]
                    })
                },1000);
            }
            //不咋用
            static getDerivedStateFromProps(props,state){
                console.log('count---getDerivedStateFromProps props==>',props)
                console.log('count---getDerivedStateFromProps state==>',state)

                return null
            }
    
            
    
            render() {
                console.log('Count---render');
                const {count} = this.state
                return (
                    <div className="list" ref="list">
                        
                        {
                            this.state.newsArr.map((n,index)=>{
                                return <div className="news" key={index}>{n}</div>
                            })
                        }
                    </div>
                )
            }
            
            getSnapshotBeforeUpdate(){
                //取内容区高度
                
                console.log('Count---getSnapshotBeforeUpdate');
                return this.refs.list.scrollHeight
            }

            //组件挂载完毕的钩子
            componentDidUpdate(preProps,preState,snapShotValue) {
                console.log('Count---componentDidUpdate',preProps,preState,snapShotValue);
                this.refs.list.scrollTop += this.refs.list.scrollHeight - snapShotValue;
            }

        }
    
        //渲染组件
        ReactDOM.render(<NewsList/>, document.getElementById('test'))
    </script>
        
</body>
</html>